/*
 * Copyright (c) 2024 iSoftStone Education Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __SMART_HOME_H__
#define __SMART_HOME_H__

// 在 smart_home_config.h 中定义
#define LIGHT_THRESHOLD  1000

#include <stdint.h>
#include <stdbool.h>
#include "lz_hardware/pinctrl.h"

#define MQ2_PIN 4
#define CAL_PPM 20 // 校准环境中PPM值
#define RL 1       // RL阻值

#define I2C_HANDLE EI2C0_M2
// #define MOTOR_PWM_HANDLE EPWMDEV_PWM6_M0
// #define LED_R_GPIO_HANDLE GPIO0_PB5
// #define LED_G_GPIO_HANDLE GPIO0_PB4
// #define LED_B_GPIO_HANDLE GPIO1_PD0
// #define GPIO_BODY_INDUCTION GPIO0_PA3
#define MOTOR_PWM_B1A GPIO0_PA3
#define MOTOR_PWM_B2A GPIO0_PB5
#define LIGHT_CTL_PIN GPIO0_PB4
#define DOOR_ENTRY_PIN EPWMDEV_PWM1_M1
#define DOOR_GARAGE_PIN EPWMDEV_PWM0_M1
#define WINDOW_PIN EPWMDEV_PWM7_M1

#define SHT30_I2C_ADDRESS 0x44
#define KEYBOARD_I2C_ADDRESS 0x20
#define BH1750_I2C_ADDRESS 0x23
#define AHT20_I2C_ADDRESS 0x38

#define KEYPAD_ROWS 4
#define KEYPAD_COLS 4
#define DEBOUNCE_DELAY_MS 20
// 定义颜色常量
#define COLOR_WHITE 0xFFFFFF
#define COLOR_OFF   0x000000

typedef struct
{
    int                 init;               //初始化状态
    int                 port;               //端口
    Pinctrl             adc;                //io总线
} iss_adc_dev_s;
typedef iss_adc_dev_s iss_mq2_dev_s;

static float m_r0; // 元件在干净空气中的阻值
static iss_mq2_dev_s m_iss_mq2 =
{
    .init = 0,
    .port = 4,
    .adc = {.gpio = MQ2_PIN, .func = MUX_FUNC1, .type = PULL_NONE, .drv = DRIVE_KEEP, .dir = LZGPIO_DIR_IN, .val = LZGPIO_LEVEL_KEEP},
};

// enum auto_command
// {
//     auto_state_on = 0x0001,
//     auto_state_off,
// };

// enum light_command
// {
//     light_state_on = 0x0101,
//     light_state_off,
// };

// enum motor_command
// {
//     motor_state_on = 0x0201,
//     motor_state_off,
// };

// enum temperature_command
// {
//     temperature_get = 0x0301,
//     humidity_get,
//     illumination_get,
//     gas_get,
// };

//GRF_SOC_CON29寄存器结构体
typedef union
{
    uint32_t value;
    struct
    {
        uint16_t grf_saradc_ana_reg_low: 4;
        uint16_t grf_saradc_vol_sel: 1;
        uint16_t grf_saradc_ana_reg_high: 11;
        struct 
        {
            uint16_t grf_saradc_ana_reg_low: 4;
            uint16_t grf_saradc_vol_sel: 1;
            uint16_t grf_saradc_ana_reg_high: 11;
        } rw;
    };
} GRF_SOC_CON29;

// 消抖状态机
typedef enum {
    DEBOUNCE_STATE_IDLE,
    DEBOUNCE_STATE_PRESS_DETECTED,
    DEBOUNCE_STATE_WAIT_RELEASE
} DebounceState;

// 门、窗位置
typedef enum {
    DOOR_ENTRY,
    DOOR_GARAGE,
    WINDOW,

    DOOR_POS_END
} DoorPos;

void i2c_dev_init(void);
int get_light_value(float *lux);
int get_tmp_hum_value(float *temperature, float *humidity);
int get_key_pressed(char *key);

void gpio_dev_init(void);
// void light_dev_init(void);
// void light_set_pwm(unsigned int duty);
// void light_set_state(bool state);
void light_control(bool state);
int door_init(void);
int door_control(int pin, bool isOpen);

void lcd_dev_init(void);
void lcd_load_ui(void);

// void body_induction_dev_init(void);
// bool body_induction_get_state();

void lcd_set_light_state(bool state);
void lcd_set_auto_state(bool state);

uint32_t mq2_dev_init(iss_mq2_dev_s *adc);
void calibrate_mq2_ppm(void);
float get_mq2_ppm(void);

#endif
